<template xmlns:font_size="http://www.w3.org/1999/xhtml">

    <div class="search">
        <Row>
            <Col>
                <Card>
                    <Row @keydown.enter.native="handleSearch">
                        <Form ref="searchForm" :model="searchForm" inline :label-width="70" class="search-form">
                            <Form-item label="行政区" prop="xzq">
                                <!--<Input type="text" v-model="searchForm.xzq" placeholder="请输入行政区" clearable style="width: 200px"/>-->
                                <treeselect v-model="searchForm.xzq" :multiple="false" :options="options" style="width: 200px" placeholder="请输入行政区"/>
                            </Form-item>
                            <Form-item label="河长姓名" prop="hzxm">
                                <Select v-model="searchForm.hzxm" style="width: 200px">
                                    <Option v-for="item in userList" :value="item.id" :key="item.id" :label="item.username">
                                        <span style="margin-right:10px;">{{ item.username }}</span>
                                        <span style="color:#ccc;">{{ item.departmentTitle }}</span>
                                    </Option>
                                </Select>
                            </Form-item>
                            <span v-if="drop">
<!--                <Form-item label="所辖河流" prop="sxhl">-->
<!--                  <Input type="text" v-model="searchForm.sxhl" placeholder="请输入所辖河流" clearable style="width: 200px"/>-->
<!--                </Form-item>-->
                <Form-item label="所辖河流" prop="riverId">
                  <Select v-model="searchForm.riverId" placeholder="请选择" clearable style="width: 200px">
                    <Option v-for="river in riverData" :value="river.id">{{river.riverName}}</Option>
                  </Select>
                </Form-item>
                <Form-item label="时间" prop="sj">
                  <DatePicker v-model="selectDate" type="daterange" format="yyyy-MM-dd" clearable @on-change="selectDateRange" placeholder="选择起始时间" style="width: 200px"></DatePicker>
                </Form-item>
              </span>
                            <Form-item style="margin-left:-35px;" class="br">
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset">重置</Button>
                                <a class="drop-down" @click="dropDown">
                                    {{dropDownContent}}
                                    <Icon :type="dropDownIcon"></Icon>
                                </a>
                            </Form-item>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="add" icon="md-refresh" type="primary">巡河任务设置</Button>
                        <Button @click="getDataList" icon="md-refresh">刷新</Button>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort" @on-selection-change="changeSelect"></Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="500">
            <Table :columns="columnsSet" :data="dataSet"></Table>
            <div slot="footer">
                <Button type="text" @click="modalVisible=false">取消</Button>
                <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
            </div>
        </Modal>
        <Modal title="巡河任务详细" v-model="lookVisible" :mask-closable='false' :width="500" :footer-hide='false'>
            <div>
            <p  style="font-size:18px;color: deepskyblue;display: inline-block" >{{this.lookData.username}}</p>
            <p  style="display: inline-block;margin-left: 19px;" >{{this.lookData.xzq}}</p>
            </div>
            <div>
            <p style="display: inline-block" >所属河流 :{{this.lookData.river_name}}</p>
            <p style="display: inline-block;margin-left: 19px;"  >任务进度 :{{this.lookData.rwjd}}</p>
            <p style="display: inline-block;margin-left: 29px;"  >频次 :{{this.lookData.frequency=='1'?'按周':'按月'}}</p>
            </div>

            <!--<Card style="width:100%;">-->
                <Table :show-header="false" width="100%" :columns="columns1" :data="data1"></Table>
            <!--</Card>-->
        </Modal>
        <!--轨迹回放-->
        <Modal :title="modalTitle" v-model="modalsVisible"  footer-hide	fullscreen :width="500">
            <div id="containerMap3" >
                <div id="containers"></div>
                <div id="containerTree1">
                    <Row>
                        <h4 style="font-size: 15px">轨迹回放控制</h4>
                    </Row>
                    <Row style="margin-top: 15px">
                        <Button type="primary" @click="startAnimation" ghost shape="circle" style="width: 43%;margin-right: 15px">开始动画</Button>
                        <Button type="primary" @click="pauseAnimation" ghost shape="circle" style="width: 43%;">暂停动画</Button>
                    </Row>
                    <Row style="margin-top: 15px">
                        <Button type="primary" @click="resumeAnimation" ghost shape="circle" style="width: 43%;margin-right: 15px">继续动画</Button>
                        <Button type="primary" @click="stopAnimation" ghost shape="circle" style="width: 43%;">停止动画</Button>
                    </Row>
                </div>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {
        getRierTaskListData,
        getDeptTrees,
        getAllUserData
    } from "@/api/index";
    import Treeselect from '@riophae/vue-treeselect'
    import MapLoader from '@/plugins/amap.js';
    import '@riophae/vue-treeselect/dist/vue-treeselect.css'
    export default {
        name: "riverTask",
        components: {
            Treeselect,

        },
        data() {
            return {
                value: null,
                options: [],
                columns1: [
                    {
                        title: '时间',
                        key: 'patrolStartTime',
                        width: 200,
                    },
                    {
                        title: '巡检里程',
                        key: 'patrolSpeed',
                        width: 100,
                    },
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        width: 130,
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "primary",
                                            size: "small",
                                            icon: "ios-create-outline"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.selectlnt(params.row);
                                            }
                                        }
                                    },
                                    "查看历史轨迹"
                                )
                            ]);
                        }
                    }
                ],
                data1: [

                ],
                loading: true, // 表单加载状态
                modalType: 0, // 添加或编辑标识
                modalVisible: false, // 添加或编辑显示
                modalsVisible: false, // 添加或编辑显示
                lookVisible:false,//查看modal
                modalTitle: "", // 添加或编辑标题
                userList:[],
                drop: false,
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                searchForm: { // 搜索框初始化对象
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "createTime", // 默认排序字段
                    riverId:"",
                    order: "desc", // 默认排序方式
                    startDate: "", // 起始时间
                    endDate: "" // 终止时间
                },
                selectDate: null,
                form: { // 添加或编辑表单对象初始化数据
                    riverId:"",
                },
                submitReiverList:[],//弹框修改得数据都保存到这里
                // 表单验证规则
                formValidate: {
                    riverId: [{ required: true, message: "请选择所属河流", trigger: "blur" }],
                },
                submitLoading: false, // 添加或编辑提交状态
                selectList: [], // 多选数据
                selectCount: 0, // 多选计数
                lookData:[],
                columns: [
                    // 表头

                    {
                        type: "index",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "行政区",
                        key: "xzq",
                        minWidth: 120,
                        sortable: false,
                    },
                    {
                        title: "河长姓名",
                        key: "username",
                        minWidth: 120,
                        sortable: false,
                    },
                    {
                        title: "所辖河流",
                        key: "river_name",
                        minWidth: 120,
                        sortable: false,
                    },
                    {
                        title: "任务进度",
                        key: "rwjd",
                        minWidth: 120,
                        sortable: false,
                    },
                    {
                        title: "频次",
                        key: "frequency",
                        minWidth: 120,
                        sortable: false,
                        align: "center",
                        render: (h, params) => {
                            let text = "";
                            let color = "";
                            if (params.row.frequency == "1") {
                                text = "周";
                                color = "volcano";
                            } else  {
                                text = "月";
                                color = "purple";
                            }
                            return h("div", [
                                h("Tag", {
                                        props: {
                                            color: color,
                                        },
                                    },
                                    text
                                )
                            ]);

                        },
                    },
                    {
                        title: "当月第几周",
                        key: "rq",
                        minWidth: 120,
                        align: "center",
                        sortable: false,
                    },
                    {
                        title: "时间",
                        key: "mon",
                        minWidth: 120,
                        sortable: false,
                    },
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        width: 200,
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "primary",
                                            size: "small",
                                            icon: "ios-create-outline"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "查看"
                                )
                            ]);
                        }
                    }
                ],
                columnsSet:[{
                    title: "职位",
                    key: "position",
                    minWidth: 120,
                    sortable: false,
                },
                    {
                        title: "巡河频次",
                        key: "frequency",
                        minWidth: 120,
                        render: (h, params) => {
                            return h("RadioGroup", {
                                props: {
                                    value: params.row.frequency,
                                    "true-value": "1",
                                    "false-value": "-1"
                                },
                                on: {
                                    "on-change": v => {
                                        this.changeStatus(params.row, v,"xhpc");
                                    }
                                }
                            },[
                                h("Radio",{props: {label: "1"}}, "按周"),
                                h("Radio",{props: {label: "2"}},"按月")
                            ]);
                        }
                    },
                    {
                        title: "巡河次数",
                        key: "numtime",
                        minWidth: 120,
                        render: (h, params) => {
                            return h("InputNumber", {
                                props: {
                                    value: params.row.numtime?params.row.numtime:0,
                                    min: 1
                                },
                                on: {
                                    "on-change": v => {
                                       this.changeStatus(params.row, v,"xhcs");
                                    }
                                }
                            });
                        }
                    }],
                dataSet: [],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            };
        },
        methods: {
            init() {
                this.getRierTaskList();
                this.getDataList();
                this.getRiverList();
            },
            addpoint:function (path) {
                let lineArr = path;
                this.lineArrs = path;
                // let lineArr = [["116.478935","39.997761"],["116.478939","39.997825"],["116.478912","39.998549"],["116.478912","39.998549"],["116.478998","39.998555"],["116.478998","39.998555"],["116.479282","39.99856"],["116.479658","39.998528"],["116.480151","39.998453"],["116.480784","39.998302"],["116.480784","39.998302"],["116.481149","39.998184"],["116.481573","39.997997"],["116.481863","39.997846"],["116.482072","39.997718"],["116.482362","39.997718"],["116.483633","39.998935"],["116.48367","39.998968"],["116.484648","39.999861"]];
                let that = this;
                this.marker = new AMap.Marker({
                    map: that.map,
                    position: lineArr[0],
                    icon: "",
                    offset: new AMap.Pixel(-20, -16),
                    autoRotation: true,
                    // angle:0,
                });

                // 绘制轨迹
                var polyline = new AMap.Polyline({
                    map: that.map,
                    path: lineArr,
                    showDir:true,
                    strokeColor: "#28F",  //线颜色
                    // strokeOpacity: 1,     //线透明度
                    strokeWeight: 3,      //线宽
                    // strokeStyle: "solid"  //线样式
                });

                var passedPolyline = new AMap.Polyline({
                    map: that.map,
                    // path: lineArr,
                    strokeColor: "#AF5",  //线颜色
                    // strokeOpacity: 1,     //线透明度
                    strokeWeight: 3,      //线宽
                    // strokeStyle: "solid"  //线样式
                });


                this.marker.on('moving', function (e) {
                    passedPolyline.setPath(e.passedPath);
                });

                that.map.setFitView();
            },

            startAnimation() {
                this.marker.moveAlong(this.lineArrs, 200);
            },

            pauseAnimation() {
                this.marker.pauseMove();
            },

            resumeAnimation() {
                this.marker.resumeMove();
            },

            stopAnimation() {
                this.marker.stopMove();
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getRierTaskList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getRierTaskList();
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                // this.getRierTaskList();
                 this.getDataList();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                // 重新加载数据
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getRierTaskList();
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            changeSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            changeStatus(row, v,type) {
                if(type==="xhcs"){
                    this.dataSet[row._index]["numtime"]=v
                }else{
                    this.dataSet[row._index]["frequency"]=v
                }

            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            getRierTaskList() {
                this.loading = true;
                // 带多条件搜索参数获取表单数据 请自行修改接口
                getRierTaskListData(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.success) {
                        this.data = res.result.content;
                        this.total = res.result.totalElements;
                    }
                });
                // 以下为模拟数据
                this.data = [
                ];
                this.total = this.data.length;
                this.loading = false;
            },
            handleSubmit() {
                let data = JSON.stringify(this.dataSet);
                this.postRequest("/riversSetUp/insertOrUpdate", {riversSetUp:data}).then(res => {
                  this.submitLoading = false;
                  if (res.success) {
                    this.$Message.success("操作成功");
                      this.searchForm.pageNumber = 1;
                      this.searchForm.pageSize = 10;
                      this.getDataList();
                    // this.getRierTaskList();
                    this.modalVisible = false;
                  }
                });
                // this.$refs.form.validate(valid => {
                //     if (valid) {
                //         this.submitLoading = true;
                //         if (this.modalType === 0) {
                //             // 添加 避免编辑后传入id等数据 记得删除
                //             delete this.form.id;
                //             // this.postRequest("请求地址", this.form).then(res => {
                //             //   this.submitLoading = false;
                //             //   if (res.success) {
                //             //     this.$Message.success("操作成功");
                //             //     this.getRierTaskList();
                //             //     this.modalVisible = false;
                //             //   }
                //             // });
                //             // 模拟请求成功
                //             this.submitLoading = false;
                //             this.$Message.success("操作成功");
                //             this.getRierTaskList();
                //             this.modalVisible = false;
                //         } else {
                //             // 编辑
                //             // this.postRequest("请求地址", this.form).then(res => {
                //             //   this.submitLoading = false;
                //             //   if (res.success) {
                //             //     this.$Message.success("操作成功");
                //             //     this.getRierTaskList();
                //             //     this.modalVisible = false;
                //             //   }
                //             // });
                //             // 模拟请求成功
                //             this.submitLoading = false;
                //             this.$Message.success("操作成功");
                //             this.getRierTaskList();
                //             this.modalVisible = false;
                //         }
                //     }
                // });
            },
            getDataList() {
                this.loading = true;
                // 带多条件搜索参数获取表单数据 请自行修改接口
                this.getRequest("/riversSetUp/getByPage", this.searchForm).then(res => {
                    this.loading = false;
                    if (res.success) {
                        this.data = res.result.records;
                        this.total = res.result.total;
                    }
                });
            },
            getRiverList() {
                let that = this;
                this.loading = true;
                //带多条件搜索参数获取表单数据 请自行修改接口
                this.getRequest("/river/getAll", this.searchForm).then(res => {
                    this.loading = false;
                    if (res.success) {
                        that.riverData = res.result;
                    }
                });
            },
            add() {
                this.modalType = 0;
                this.modalTitle = "巡河任务设置";
                // this.$refs.form.resetFields();
                //查询巡河设置信息内容
                this.getRequest("/riversSetUp/getAll").then(res => {
                  this.dataSet=res.result;
                });
                this.modalVisible = true;
            },
            selectlnt(v) {

                this.modalTitle = "轨迹回放查看";
                /* this.$refs.form.resetFields();
                 // 转换null为""
                 for (let attr in v) {
                   debugger
                   if (v[attr] === null) {
                     v[attr] = "";
                   }
                 }
                 let str = JSON.stringify(v);
                 let data = JSON.parse(str);
                 this.form = data;*/

                let data = JSON.parse(v.locus);

                this.modalsVisible = true;
                let that = this;
                MapLoader().then(AMap => {
                    that.map =new AMap.Map("containers", {
                        /*zoom:11.45,
                        center:[116.70,40.16],*/
                        pitch: 0,
                        viewMode: '3D',
                        resizeEnable: true,
                        /*layers: [
                          // 卫星
                          // new AMap.TileLayer.Satellite(),
                          // 路网
                          new AMap.TileLayer.RoadNet()
                        ],*/
                    });
                    that.addpoint(data);
                }, e => {
                    console.log('地图加载失败' ,e)
                })
            },
            edit(v) {
                for (let attr in v) {
                    if (v[attr] === null ||v[attr] === '-' ) {
                        v[attr] = "";
                    }
                }
                let str = JSON.stringify(v);
                let data = JSON.parse(str);
                this.lookData=data;
                this.getRequest("/riverTourHistory/queryRiverHistoryByDate/",data).then(res => {
                    if(res.result && res.result.length>0){
                        res.result.map(item=>{
                            if(item.patrolEndTime &&item.patrolEndTime.length>8){
                                item.patrolStartTime=item.patrolStartTime+'-'+item.patrolEndTime.slice(-8)
                            }
                        })
                    }
                  this.data1=res.result;
                });
                this.lookVisible = true;
            },
            getUserAll() {
                getAllUserData().then(res => {
                    if (res.success) {
                        this.userList = res.result;
                    }
                });
            },
            deptsTree(){
                getDeptTrees().then(res => {
                    if (res.success) {
                        this.options = res.result;
                    }
                });
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    // 记得确认修改此处
                    content: "您确认要删除 " + v.name + " ?",
                    loading: true,
                    onOk: () => {
                        // 删除
                        // this.deleteRequest("请求地址，如/deleteByIds/" + v.id).then(res => {
                        //   this.$Modal.remove();
                        //   if (res.success) {
                        //     this.$Message.success("操作成功");
                        //     this.getRierTaskList();
                        //   }
                        // });
                        // 模拟请求成功
                        this.$Message.success("操作成功");
                        this.$Modal.remove();
                        this.getRierTaskList();
                    }
                });
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    loading: true,
                    onOk: () => {
                        let ids = "";
                        this.selectList.forEach(function(e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        // 批量删除
                        // this.deleteRequest("请求地址，如/deleteByIds/" + ids).then(res => {
                        //   this.$Modal.remove();
                        //   if (res.success) {
                        //     this.$Message.success("操作成功");
                        //     this.clearSelectAll();
                        //     this.getRierTaskList();
                        //   }
                        // });
                        // 模拟请求成功
                        this.$Message.success("操作成功");
                        this.$Modal.remove();
                        this.clearSelectAll();
                        this.getRierTaskList();
                    }
                });
            }
        },
        mounted() {
            this.init();
            this.getUserAll();
            this.deptsTree();
        }
    };
</script>
<style lang="less">
    #containerMap3 {
    #containers {
        position: absolute;
        left: 0px;
        right: 0px;
        top: 0px;
        bottom: 0px;
        margin:0;
        z-index: 5;
    }
    #containerTree1 {
        z-index: 100;
        padding: 10px 15px;
        position: absolute;
        right: 15px;
        bottom: 15px;
        width: 250px;
        border-radius: 5px;
        box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
        background: #FFFFFF;
        min-height: 20%;
        max-height: 20%;
        overflow:auto
    }
    }
    .search{
        .operation {
            margin-bottom: 2vh;
        }
        .select-count {
            font-size: 13px;
            font-weight: 600;
            color: #40a9ff;
        }
        .select-clear {
            margin-left: 10px;
        }
        .page {
            margin-top: 2vh;
        }
        .drop-down {
            font-size: 13px;
            margin-left: 5px;
        }
    }
</style>